//
//  UIColor+Ext.swift
//  swiftDemo
//
//  Created by Gerry on 2021/11/9.
//

import UIKit

extension UIColor {
    convenience init(r: CGFloat, g : CGFloat, b : CGFloat) {
        self.init(red: r / 255.0, green: b / 255.0, blue: g / 255.0, alpha: 1)
    }
    
    convenience init(hexString: String) {
            let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
            let scanner = Scanner(string: hexString)
             
            if hexString.hasPrefix("#") {
                scanner.scanLocation = 1
            }
             
            var color: UInt32 = 0
            scanner.scanHexInt32(&color)
             
            let mask = 0x000000FF
            let r = Int(color >> 16) & mask
            let g = Int(color >> 8) & mask
            let b = Int(color) & mask
             
            let red   = CGFloat(r) / 255.0
            let green = CGFloat(g) / 255.0
            let blue  = CGFloat(b) / 255.0
             
            self.init(red: red, green: green, blue: blue, alpha: 1)
        }
}
